package us.wili.dev.client.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.catalina.servlet4preview.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import us.wili.dev.client.model.req.AddShopCartReq;
import us.wili.dev.client.model.req.DeleteReq;
import us.wili.dev.client.model.req.upShopCartReq;
import us.wili.dev.client.model.rsp.ShopcartRsp;
import us.wili.dev.client.service.ClientShopCartService;
import us.wili.dev.common.model.ResponseDto;
import us.wili.dev.common.util.ResponseUtil;
import us.wili.dev.model.entity.Shopcart;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.List;
@Validated
@RestController
@RequestMapping("/api/shopcat")
@Api(tags = "用户")
public class ShopCartController {
    @Autowired
    private ClientShopCartService clientShopCartService;

    @ApiOperation(value = "查看购物车", nickname = "shopcart")
    @RequestMapping(value = "/shopcart", method = RequestMethod.GET)
    public ResponseDto<ShopcartRsp> shopcart(Long id){
        List<ShopcartRsp> rsp =clientShopCartService.shopcart(id);
        return ResponseUtil.getSuccessRsp(rsp);
    }

    @ApiOperation(value = "加入购物车", nickname = "Joinshopcart")
    @RequestMapping(value = "/Joinshopcart", method = RequestMethod.POST)
    public ResponseDto Joinshopcart(@Valid @RequestBody AddShopCartReq req,HttpServletRequest request,HttpServletResponse response){
        Shopcart shopcart = clientShopCartService.Joinshopcart(req);
        return ResponseUtil.getSuccessRsp(shopcart);
    }

    @ApiOperation(value = "更改购物车数量", nickname = "upshopcart")
    @RequestMapping(value = "/upshopcart", method = RequestMethod.POST)
    public ResponseDto upshopcart(@Valid @RequestBody upShopCartReq req,HttpServletRequest request,HttpServletResponse response){
        Shopcart shopcart = clientShopCartService.upshopcart(req);
        return ResponseUtil.getSuccessRsp(shopcart);
    }

    @ApiOperation(value = "购物车删除商品", nickname = "delete")
    @RequestMapping(value = "/delete", method = RequestMethod.GET)
    public ResponseDto delete(Long id) {
        clientShopCartService.delete(id);
        return new ResponseDto();
    }

    @ApiOperation(value = "批量购物车删除商品", nickname = "deletes")
    @RequestMapping(value = "/deletes", method = RequestMethod.POST)
    public ResponseDto deletes(@Valid @RequestBody DeleteReq req,HttpServletRequest request,HttpServletResponse response) {
        clientShopCartService.deletes(req);
        return new ResponseDto();
    }
}
